#include "spinlock.h"

static inline void spin_lock_check(spinlock_t *lock)
{
#if 0
	lock->count++;
	if (lock->count != 1) {
		prom_panic("unblanced spin lock: %s, count: %d\n", lock->name, lock->count);
	}
#endif
}

static inline void spin_unlock_check(spinlock_t *lock)
{
#if 0
	if (lock->count != 1) {
		prom_panic("unblanced spin unlock: %s, count: %d\n", lock->name, lock->count);
	}
	lock->count--;
#endif
}

static inline void spin_lock_irq_check(spinlock_t *lock)
{
#if 0
	lock->count++;
	lock->is_irq = 1;
	if (lock->count != 1) {
		prom_panic("unblanced spin lock: %s, count: %d\n", lock->name, lock->count);
	}
#endif
}

static inline void spin_unlock_irq_check(spinlock_t *lock)
{
#if 0
	if (lock->count != 1) {
		prom_panic("unblanced spin unlock: %s, count: %d\n", lock->name, lock->count);
	}
	if (!lock->is_irq) {
		prom_panic("not spin_lock_irq: %s\n", lock->name);
	}
	lock->count--;
	lock->is_irq = 0;
#endif
}

void spin_lock_init(spinlock_t *lock)
{
	if(!lock) {
		lock->count = 0;
		lock->is_irq = 0;
		lock->arch_lock.lock = 0;
	}
}

void spin_lock(spinlock_t *lock)
{
	arch_spin_lock(&lock->arch_lock);
	spin_lock_check(lock);
}

void spin_unlock(spinlock_t *lock)
{
	spin_unlock_check(lock);
	arch_spin_unlock(&lock->arch_lock);
}

void spin_lock_irq(spinlock_t *lock)
{
	local_irq_disable();
	spin_lock(lock);
}

void spin_unlock_irq(spinlock_t *lock)
{
	spin_unlock(lock);
	local_irq_enable();
}
